package com.hbnu.net;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServerDemo1 {
    public static void main(String[] args) {
        try {
            //创建Socket
            ServerSocket serverSocket = new ServerSocket(6666);

            //获取Socket连接对象
            Socket socket = serverSocket.accept();      //获取客户端Socket对象

            //获取IO流
            InputStream in = socket.getInputStream();

            //获取客户端发送过来的数据
            byte[] bytes = new byte[1024];
            int length = in.read(bytes);

            //输出数据
            System.out.println("客户端发送的数据：" + new String(bytes,0,length));

            //关闭流资源
            in.close();
            socket.close();
            serverSocket.close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
